package cn.com.chengmandian.bike.utils;

import cn.com.chengmandian.bike.model.EbSettingAd;
import cn.com.chengmandian.bike.model.vo.PriceRuleVo;
import cn.com.chengmandian.core.web.utils.SpringContextUtil;
import com.github.benmanes.caffeine.cache.Cache;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class CaffeineUtils {
    private static Cache<String, Object> adListCaffeineCache;
    private static Cache<String, Object> priceRuleCaffeineCache;

    static {
        adListCaffeineCache = (Cache<String, Object>) SpringContextUtil.getBean("adListCaffeineCache");
        priceRuleCaffeineCache = (Cache<String, Object>) SpringContextUtil.getBean("priceRuleCaffeineCache");
    }

    public static void putAdList(String key, List<EbSettingAd> adList) {
        try {
            adListCaffeineCache.put(key, adList);
        } catch (Exception e) {
            log.error("CaffeineUtils error!", e);
        }
    }

    public static List<EbSettingAd> getAdList(String key) {
        try {
            return (List<EbSettingAd>)adListCaffeineCache.asMap().get(key);
        } catch (Exception e) {
            log.error("CaffeineUtils error!", e);
        }
        return null;
    }

    public static void removeAdList(String key) {
        try {
            adListCaffeineCache.invalidate(key);
        } catch (Exception e) {
            log.error("CaffeineUtils error!", e);
        }
    }

    public static void putPriceRule(String key, PriceRuleVo vo) {
        try {
            priceRuleCaffeineCache.put(key, vo);
        } catch (Exception e) {
            log.error("CaffeineUtils error!", e);
        }
    }

    public static PriceRuleVo getPriceRule(String key) {
        try {
            return (PriceRuleVo)priceRuleCaffeineCache.asMap().get(key);
        } catch (Exception e) {
            log.error("CaffeineUtils error!", e);
        }
        return null;
    }

    public static void removePriceRule(String key) {
        try {
            priceRuleCaffeineCache.invalidate(key);
        } catch (Exception e) {
            log.error("CaffeineUtils error!", e);
        }
    }
}
